/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package praktika;
import javax.swing.*;
import java.util.Random;

public class Main {


    public static int[][] mat1;
    public static int[][] mat2;
    public static int[][] result;
    public static int size = 0;


    public static void main(String args[]) {
        enter();
        print(mat1);
        print(mat2);
        long time = System.nanoTime();
        multplication();
        System.out.print("\nДетерминант результата = " + determinant(result));
        addiction();
        System.out.print("\nДетерминант результата = " + determinant(result));
        System.out.print("\nВремя выполнения - " + Long.toString((System.nanoTime() - time) / 1000000) + " милисекунд.");

    }

    public static void enter() {
        /*Scanner in = new Scanner(System.in);
        System.out.print("\nВведите размер матриц: ");
        size = in.nextInt();
        Random rand = new Random();
        mat1 = new int[size][size];
        mat2 = new int[size][size];
        result = new int[size][size];
        for (int i = 0; i < size; i++)
            for (int j = 0; j < size; j++)
                mat1[i][j] = rand.nextInt(20)-10;
        for (int i = 0; i < size; i++)
            for (int j = 0; j < size; j++)
                mat2[i][j] = rand.nextInt(20)-10;

        */
        mat1 = new int[size][size];
        mat2 = new int[size][size];
        result = new int[size][size];
        for (int i = 0; i < size; i++)
            for (int j = 0; j < size; j++) {
                if (Frame.areas1[i][j].getText().equals("")) mat1[i][j] = 0;
                else
                    mat1[i][j] = Integer.parseInt(Frame.areas1[i][j].getText());
            }
        for (int i = 0; i < size; i++)
            for (int j = 0; j < size; j++) {
                if (Frame.areas2[i][j].getText().equals("")) mat2[i][j] = 0;
                else
                    mat2[i][j] = Integer.parseInt(Frame.areas2[i][j].getText());
            }
    }

    public static void randomfill() {
        Random rand = new Random();
        mat1 = new int[size][size];
        mat2 = new int[size][size];
        result = new int[size][size];
        for (int i = 0; i < size; i++)
            for (int j = 0; j < size; j++) {
                mat1[i][j] = rand.nextInt(500) - 250;
                Frame.areas1[i][j].setText(Integer.toString(mat1[i][j]));
            }
        for (int i = 0; i < size; i++)
            for (int j = 0; j < size; j++) {
                mat2[i][j] = rand.nextInt(500) - 250;
                Frame.areas2[i][j].setText(Integer.toString(mat2[i][j]));
            }
    }

    public static void print(int[][] matrix) {
        //System.out.print("\nВаша матрица : \n\n");
        for (int i = 0; i < size; i++)
            for (int j = 0; j < size; j++) {
                /*System.out.print(matrix[i][j] + " ");
                if (j == size - 1) System.out.print("\n");*/

                Frame.areas3[i][j].setText(Integer.toString(matrix[i][j]));
            }
    }

    public static void multplication() {
        for (int i = 0; i < size; i++)
            for (int j = 0; j < size; j++) {
                try {
                    Thread.sleep(Frame.latency);
                } catch (InterruptedException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                result[i][j] = mult_elem(i, j);
                JOptionPane.showMessageDialog(null, "Шаг №  " + (i + j) + " завершился. Ci,j = " + result[i][j]);
                Frame.areas3[i][j].setText(String.valueOf(result[i][j]));
            }
        //System.out.print("\nРезультат умножения: \n");
        print(result);
    }

    private static int mult_elem(int r, int z) {
        int res = 0;
        for (int i = 0; i < size; i++) {
            res += mat1[r][i] * mat2[i][z];
        }


        return res;

    }

    static void addiction() {
        for (int i = 0; i < size; i++)
            for (int j = 0; j < size; j++) {

                try {
                    Thread.sleep(Frame.latency);
                } catch (InterruptedException e) {
                    e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
                }
                result[i][j] = mat1[i][j] + mat2[i][j];
                JOptionPane.showMessageDialog(null, "Шаг №  " + (i + j) + " завершился. Ci,j = " + result[i][j]);
                Frame.areas3[i][j].setText(String.valueOf(result[i][j]));
            }
        // System.out.print("\nРезультат сложения: \n");
        print(result);
    }

    public static double determinant(int[][] result) {

        double det = 0;

        if (result.length == 1) {
            det = result[0][0];
            return det;
        }

        if (result.length == 2) {
            det = result[0][0] * result[1][1] - result[0][1] * result[1][0];
            return det;
        }

        for (int i = 0; i < result[0].length; i++) {
            int temp[][] = new int[result.length - 1][result[0].length - 1];

            for (int j = 1; j < result.length; j++) {
                System.arraycopy(result[j], 0, temp[j - 1], 0, i);
                System.arraycopy(result[j], i + 1, temp[j - 1], i, result[0].length - i - 1);
            }
            det += result[0][i] * Math.pow(-1, i) * determinant(temp);
        }

        return det;
    }

}